import time
import logging
import os
from common.consts import BASE_PATH
from config.config import Config


class Log:
    @classmethod
    def _config(cls):
        c = Config()
        log_dir = os.path.join(BASE_PATH, c.get_runtime("log_dir"))
        now = time.strftime('%Y%m%d', time.localtime(time.time()))
        log_file = os.path.join(log_dir, now + ".log")

        cls.logger = logging.getLogger()
        cls.logger.setLevel(eval("logging." + c.get_runtime("log_level").upper()))

        file_handler = logging.FileHandler(log_file, mode="a", encoding='utf-8')
        console_handler = logging.StreamHandler()

        format = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
        file_handler.setFormatter(format)
        console_handler.setFormatter(format)

        cls.logger.addHandler(file_handler)
        cls.logger.addHandler(console_handler)

    @classmethod
    def get_logger(cls):
        cls._config()
        return cls.logger


if __name__ == '__main__':
    log = Log.get_logger()
    log.error('sdafadsadada')
